package com.ruoyi.order.appStoreUtil;

import java.io.ByteArrayInputStream;
import java.security.PublicKey;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.interfaces.ECPublicKey;
import java.util.Base64;
import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ruoyi.common.utils.date.DateUtils;

public class AppStoreUtil {

	/**
	 * 验证签名
	 * @param decodedJWT
	 * @return
	 * @throws CertificateException
	 */ 		
	public static JSONObject verifyAndGet(String jws) throws CertificateException {
		DecodedJWT decodedJWT = JWT.decode(jws);
		// 拿到 header 中 x5c 数组中第一个
		String header = new String(java.util.Base64.getDecoder().decode(decodedJWT.getHeader()));
		String x5c = JSONObject.parseObject(header).getJSONArray("x5c").getString(0);

		// 获取公钥
		PublicKey publicKey = getPublicKeyByX5c(x5c);

		// 验证 token
		Algorithm algorithm = Algorithm.ECDSA256((ECPublicKey) publicKey, null);

		algorithm.verify(decodedJWT);

		// 解析数据
		JSONObject payload = JSONObject.parseObject(new String(java.util.Base64.getDecoder().decode(decodedJWT.getPayload())));
		return payload;

	}

	/**
	 * 获取公钥
	 * @param x5c
	 * @return
	 * @throws CertificateException
	 */
	private static PublicKey getPublicKeyByX5c(String x5c) throws CertificateException {
		byte[] x5c0Bytes = java.util.Base64.getDecoder().decode(x5c);
		CertificateFactory fact = CertificateFactory.getInstance("X.509");
		X509Certificate cer = (X509Certificate) fact.generateCertificate(new ByteArrayInputStream(x5c0Bytes));
		return cer.getPublicKey();
	}
	
	public static void main(String[] args) {
		DecodedJWT decode = JWT.decode("eyJhbGciOiJFUzI1NiIsIng1YyI6WyJNSUlFTURDQ0E3YWdBd0lCQWdJUWZUbGZkMGZOdkZXdnpDMVlJQU5zWGpBS0JnZ3Foa2pPUFFRREF6QjFNVVF3UWdZRFZRUURERH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b1h0MlFmeUZNbStZaGlkRGtMRjF2TFVhZ002QmdENTZLeUtBPT0iXX0.eyJ0cmFuc2FjdGlvbklkIjoiNzAwMDAxNjA1ODI4MTQwIiwib3JpZ2luYWxUcmFuc2FjdGlvbklkIjoiNzAwMDAxNjA1ODI4MTQwIiwiYnVuZGxlSWQiOiJhcHAubWlkYS5jb20iLCJwcm9kdWN0SWQiOiI4MTQiLCJwdXJjaGFzZURhdGUiOjE3MjM1NjQ5NjUwMDAsIm9yaWdpbmFsUHVyY2hhc2VEYXRlIjoxNzIzNTY0OTY1MDAwLCJxdWFudGl0eSI6MSwidHlwZSI6Ik5vbi1Db25zdW1hYmxlIiwiaW5BcHBPd25lcnNoaXBUeXBlIjoiUFVSQ0hBU0VEIiwic2lnbmVkRGF0ZSI6MTcyMzY0ODIwODQ2MCwicmV2b2NhdGlvblJlYXNvbiI6MCwicmV2b2NhdGlvbkRhdGUiOjE3MjM2NDcyNzIwMDAsImVudmlyb25tZW50IjoiUHJvZHVjdGlvbiIsInRyYW5zYWN0aW9uUmVhc29uIjoiUFVSQ0hBU0UiLCJzdG9yZWZyb250IjoiQ0hOIiwic3RvcmVmcm9udElkIjoiMTQzNDY1IiwicHJpY2UiOjQ2ODAwMCwiY3VycmVuY3kiOiJDTlkifQ.sHkPPJbaqaYn0fJB3ZZqfM9aspYyJccRrJ3ia4EAVyNtXxCUIhrlagDj3O_JovSYuE7X1bRLo9IQVKIgmjY7wQ");
       String decodeString = new String(Base64.getDecoder().decode(decode.getPayload()));
       System.out.println(decodeString);
//		long time = 1723712744562L;
//		System.out.println(new Date(time));
	}
}
